랜덤 색상 생성기
✒️ 2025-05-16 12:35 내용 수정
- Javascript로 랜덤 색상을 만드는 함수를 작성했다.
- 색상코드 : RGB 타입, RED, GREN, BLUE 3가지 색을 섞어서 색상을 조합한다.
- 0~255까지 숫자로 표현을 한다.
#000000형태로 반환되어 CSS의 color attribute에 바로 적용시킬 수 있다.
function generateHex(){
const num = Math.floor( Math.random() * 256 ); // 0~255 안에서 숫자가 나옴
const hex = num.toString(16).padStart(2, '0').toUpperCase();
return hex;
}
export default function generateColorCode(){
const colorCode = `#${generateHex()}${generateHex()}${generateHex()}`
return colorCode;
}